<?php
class asterisk {
    
    public $host;
    public $port;
    public $user;
    public $pass;
    
    public $timeout;
    
    public $connect;
    
    public $phone;
    
    
    function asterisk($host, $port, $user, $pass, $timeout) {
        
        $this->connect = @fsockopen($host, $port, $errno, $errstr, $timeout);

        if (!$this->connect) {
            
            echo "ERROR: $errstr (ERROR N: $errno)\n";
            
        } else {
        
            $login = "Action: login\r\n";
            $login .= "Username: $user\r\n";
            $login .= "Secret: $pass\r\n";
            $login .= "Events: Off\r\n";
            $login .= "\r\n";
            fwrite($this->connect,$login);
        
            $manager_version    = fgets($this->connect);
            $cmd_response       = fgets($this->connect);
            $response           = fgets($this->connect);
            $blank_line         = fgets($this->connect);
        
            if (substr($response,0,9) == "Message: ") {
                
                $loginresponse = trim(substr($response,9));
                if (!$loginresponse == "Authentication Accepted") {
                    
                    echo "-- Unable to log in: $loginresponse\n";
                    fclose($this->connect);
                    exit(0);
                    
                } else {
                    
                    //echo "-- Success to log in: $loginresponse\n";
                    
                }
            } else {
                
                echo "Unexpected response: $response\n";
                fclose($this->connect);
                exit(0);
                
            }
        }
    }
            
    function getExtensions() {
        
        $checkpeer = "Action: Command\r\n";
        $checkpeer .= "Command: core show hints \r\n";
        $checkpeer .= "\r\n";
        fwrite($this->connect, $checkpeer);
        $line = trim(fgets($this->connect));
        
        $found_entry = false;
        $table       = '<table id="box-table-b" style="float:left;">';
        $i=0;
        while ($line != "--END COMMAND--") {
            if (($i-4)%28==0) {$table.="</td><table style=\"float:left;\" id=\"box-table-b\">"; } ;
            switch (true) {
        
                case strpos($line,"State:Idle"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="green">თავისუფალი</font></td>';
                    $table .= '</tr>';
                    break;
        
                case strpos($line,"State:InUse"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="#CC9900">საუბრობს</font></td>';
                    $table .= '</tr>';
                    break;
        
                case strpos($line,"State:Unavailable"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="red">მიუწვდომელი</font></td>';
                    $table .= '</tr>';
                    break;
                case strpos($line,"State:RINGING"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="red">ზარია</font></td>';
                    $table .= '</tr>';
                    break;
        
                case strpos($line,"State:Unknow"):
                    $table .= '<tr>';
                    $table .= '<td>'.substr($line, 0, 3).'</td>';
                    $table .= '<td><font size="2" color="red">უცნობი</font></td>';
                    $table .= '</tr>';
                    break;
            }
        
            $i++;
             
            $line = trim(fgets($this->connect));
        }
        
        $table .= '</table>';
        echo $table;
        fclose($this->connect);
        exit(0);
        
    } 
    
    function blackListAdd($phone) {
    
        $command = "Action: Command\r\n";
        $command .= "Command: database put blacklist $phone \"test\" \r\n";
        $command .= "\r\n";
        fwrite($this->connect,$command);
        
        fclose($this->connect);
        exit(0);
    
    }
    
    function blackListRemove($phone) {
    
        $command = "Action: Command\r\n";
        $command .= "Command: database del blacklist $phone \r\n";
        $command .= "\r\n";
        fwrite($this->connect,$command);
    
        fclose($this->connect);
        exit(0);
    
    }
    function QueueStatus($phone) {
        $command = "Action: QueueStatus\r\n";
        $command .= "Queue: $phone\r\n";
        $command .= "\r\n";
        fwrite($this->connect,$command);
        $line = trim(fgets($this->connect)); 
        $i=-1;
        
        while ($line != "Event: QueueStatusComplete") {
            $ar=split(":", $line);
            $arr[$i][$ar[0]]=$ar[1];
             $line = trim(fgets($this->connect));                          
             if ($line=='') {                 
                 $line = trim(fgets($this->connect));
                 $i++;
             }

            
            
        };
        
        return $arr;
        fclose($this->connect);
        exit(0);
    }
        
}
